package com.lanou3g.demo.controller;

import com.lanou3g.demo.controller.params.ServiceParams;
import com.lanou3g.demo.entity.Account;
import com.lanou3g.demo.entity.Cost;
import com.lanou3g.demo.entity.Service;
import com.lanou3g.demo.service.IServiceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Past;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    private IServiceService serviceService;

    /**
     * 查询业务账号表的信息
     *
     * @return
     */
    @RequestMapping("/findAllService")
    @ResponseBody
    public List<Service> findAllService() {
        return serviceService.findAllService();
    }


    // 更改状态
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Integer updateStatus(HttpServletRequest req) {
        String serviceId = req.getParameter("serviceId");
        Integer updateStatus = serviceService.updateStatus(Integer.valueOf(serviceId));
        return updateStatus;
    }

    @RequestMapping("/delStatus")
    @ResponseBody
    public Integer delStatus(HttpServletRequest req) {
        String serviceId = req.getParameter("serviceId");
        Integer delStatus = serviceService.delStatus(Integer.valueOf(serviceId));
        return delStatus;
    }

    @RequestMapping(value = "/findByOne",method = RequestMethod.POST)
    @ResponseBody
    public List<Service> findByOne(@RequestBody ServiceParams serviceParams) {
        List<Service> byOne = serviceService.findByOne(serviceParams);
       // System.out.println("***************************");
       // byOne.forEach(System.out::println);
        return byOne;
    }
    //查询身份证号获取账务账号
    @RequestMapping(value = "/findAccountName")
   @ResponseBody
    public List<Account> findAccountName(@RequestBody Account account){
        try {
            List<Account> accountName = serviceService.findAccountName(account);
            accountName.forEach(System.out::println);
            return accountName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
   //添加业务账号的信息
    @RequestMapping("/addService")
    @ResponseBody
    public Integer addService(@RequestBody Service service )  {
        System.out.println(service+"**************");
        Integer addService = serviceService.addService(service);
        System.out.println(addService);
       return addService;

    }
    @RequestMapping("/findCostName")
    @ResponseBody
    public List<Cost> findCostName(){
        List<Cost> costName = serviceService.findCostName();
        costName.forEach(System.out::println);
        return costName;
    }

    @RequestMapping("/updateName")
    @ResponseBody
    public String updateName(@RequestBody Service service){
         serviceService.updateName(service);
         /*
         {
            "status": true
         }
          */
         return "success";
    }

}
